home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / machack / Hacks97 / NewsTicker.sit / NewsTicker / source code / Ticker.cp < prev    next >
Text File  |  1997-06-26  |  6KB  |  266 lines

  1. /*------------------------------------------------------------------------------
  2. #
  3. #    NewsTicker, my Hack for 1997
  4. #
  5. #    Ticker.cp    -    the application shell to do cool stuff in
  6. #
  7. #    based on sample source code from long, long ago
  8. #
  9. ------------------------------------------------------------------------------*/
  10.  
  11. #include <PLStringFuncs.h>        /* some special string handling stuff */
  12. #include <strings.h>
  13. #include <Power.h>
  14. #include <OpenTransport.h>
  15. #include "TickerGlobals.h"        /* bring in all the #defines for Ticker */
  16. #include "TickerWindowHandler.h"
  17. #include "TickerAppEvts.h"
  18.  
  19. /* The "g" prefix is used to emphasize that a variable is global. */
  20. Boolean gHasPowerManager;
  21.  
  22. /* Prototypes of the routines that actually do the work */
  23. void EventLoop( void );
  24. void DoEvent( EventRecord *event );
  25. void DoMenuCommand( long menuResult );
  26. void Initialize( void );
  27.  
  28. #pragma segment Main
  29. main()
  30. {
  31.     SetApplLimit(GetApplLimit()-32768); //give an additional 32K to stack
  32.     MaxApplZone();                    /* expand the heap so code segments load at the top */
  33.  
  34.     Initialize();                    /* initialize the program */
  35.     
  36.     EventLoop();                    /* call the main event loop */
  37. }
  38.  
  39.  
  40. /*    Get events forever, and handle them by calling DoEvent.
  41.     Get the events by calling WaitNextEvent. */
  42.  
  43. #pragma segment Main
  44. void EventLoop()
  45. {
  46.     Boolean        gotEvent;
  47.     EventRecord    event;
  48.     WindowPtr    aWindow;
  49.     RgnHandle    mouseRgn = NewRgn();
  50.     
  51.     InitWHandler();
  52.     
  53.     SetRectRgn (mouseRgn, -32767, -32767, 32767, 32767);
  54.     
  55.     do {
  56.         
  57.         if (gHasPowerManager)
  58.         {
  59.             //IdleUpdate ();
  60.         }
  61.         PollWHandler(event.where);
  62.         
  63.         gotEvent = WaitNextEvent(everyEvent, &event, 1, nil);
  64.         
  65.         if ( gotEvent ) {
  66.             DoEvent(&event);
  67.         }
  68.     } while ( gDoneFlag );    /* loop forever; we quit via ExitToShell */
  69.     
  70.     gotEvent = true;
  71.     CloseWHandler();
  72. } /*EventLoop*/
  73.  
  74. #pragma segment Main
  75. void DoEvent(EventRecord* event)
  76. {
  77.  
  78.     short        part, err;
  79.     WindowPtr    window;
  80.     Boolean        hit;
  81.     char        key;
  82.     Point        aPoint;
  83.  
  84.     switch ( event->what ) {
  85.         case 0:
  86.             break;
  87.         case kHighLevelEvent:
  88.             err = AEProcessAppleEvent( event ) ;
  89.             break;
  90.         case mouseDown:
  91.             part = FindWindow(event->where, &window);
  92.             switch ( part ) {
  93.                 case inMenuBar:                /* process a mouse menu command (if any) */
  94.                     DoMenuCommand(MenuSelect(event->where));
  95.                     break;
  96.                 case inSysWindow:            /* let the system handle the mouseDown */
  97.                     SystemClick(event, window);
  98.                     break;
  99.             }
  100.             break;
  101.         case keyDown:
  102.         case autoKey:                        /* check for menukey equivalents */
  103.             key = event->message & charCodeMask;
  104.             if ( event->modifiers & cmdKey )            /* Command key down */
  105.                 if ( event->what == keyDown ) {
  106.                     DoMenuCommand(MenuKey(key));
  107.                 }
  108.             break;
  109.         case osEvt:
  110.             switch ((event->message >> 24) & 0x0FF) {        /* high byte of message */
  111.                 case kSuspendResumeMessage:        /* suspend/resume is also an activate/deactivate */
  112.                     gInBackground = (event->message & kResumeMask) == 0;
  113.                     break;
  114.             }
  115.             break;
  116.     }
  117. } /*DoEvent*/
  118.  
  119. /*    This is called when an item is chosen from the menu bar (after calling
  120.     MenuSelect or MenuKey). It performs the right operation for each command.
  121.     It is good to have both the result of MenuSelect and MenuKey go to
  122.     one routine like this to keep everything organized. */
  123.  
  124. #pragma segment Main
  125.  
  126. static pascal Boolean AboutAlertFilter(DialogPtr pDialog,
  127.                                     EventRecord *macEvent,
  128.                                     short *itemHit)
  129. {
  130.     GrafPtr    theport;
  131.     
  132.     GetPort(&theport);
  133.     JustHandleWindow();    //scroll the window below the about window
  134.     SetPort(theport);
  135.     
  136.     if (macEvent->what==keyDown)
  137.     {
  138.         switch (macEvent->message & charCodeMask)
  139.             {
  140.             case 0x03:        // enter key
  141.             case 0x0D:        // return key
  142.                 *itemHit = 1;
  143.                 return true;
  144.                 break;
  145.             default:
  146.                 return false;
  147.             }
  148.     }
  149.     return false;
  150. }
  151.  
  152. static void DoTheAlert(void)
  153. {
  154.     ModalFilterUPP    OurFilterUPP;
  155.     OurFilterUPP = NewModalFilterProc(AboutAlertFilter);
  156.     Alert(128, OurFilterUPP);
  157.     DisposeRoutineDescriptor(OurFilterUPP);
  158. }
  159.  
  160. void DoMenuCommand(long menuResult)
  161. {
  162.     short        menuID;                /* the resource ID of the selected menu */
  163.     short        menuItem;            /* the item number of the selected menu */
  164.     short        itemHit;
  165.     Str255        daName;
  166.     short        daRefNum;
  167.     Boolean        handledByDA;
  168.  
  169.     menuID = HiWord(menuResult);    /* use macros for efficiency to... */
  170.     menuItem = LoWord(menuResult);    /* get menu item number and menu number */
  171.     switch ( menuID ) {
  172.         case mApple:
  173.             switch ( menuItem ) {
  174.                 case iAbout:        /* bring up alert for About */
  175.                     DoTheAlert();
  176.                     break;
  177.                 default:            /* all non-About items in this menu are DAs */
  178.                     /* type Str255 is an array in MPW 3 */
  179.                     GetMenuItemText(GetMenuHandle(mApple), menuItem, daName);
  180.                     daRefNum = OpenDeskAcc(daName);
  181.                     break;
  182.             }
  183.             break;
  184.         case mFile:
  185.             switch ( menuItem ) {
  186.                 case iConfigure:
  187.                     DoConfigure ();
  188.                     break;
  189.                 case iQuit:
  190.                     gDoneFlag = false;
  191.                     break;
  192.             }
  193.             break;
  194.         case mEdit:                    /* call SystemEdit for DA editing & MultiFinder */
  195.             handledByDA = SystemEdit(menuItem-1);    /* since we don’t do any Editing */
  196.             break;
  197.     }
  198.     HiliteMenu(0);                    /* unhighlight what MenuSelect (or MenuKey) hilited */
  199. } /*DoMenuCommand*/
  200.  
  201.  
  202. #pragma segment Initialize
  203.  
  204. static Boolean GotPowerManager( void )
  205. {
  206.     long pmgrAttributes;
  207.     Boolean routinesExist;
  208.  
  209.     routinesExist = false;
  210.     if (Gestalt(gestaltPowerMgrAttr, &pmgrAttributes)==noErr)
  211.         if (pmgrAttributes & (1<<gestaltPMgrDispatchExists))
  212.             if (PMSelectorCount() >= 7) /* do the first 8 routines exist? */
  213.                routinesExist = true;
  214.     return routinesExist;
  215. }
  216.  
  217.  
  218. void Initialize()
  219. {
  220.     Handle        menuBar;
  221.     WindowPtr    window;
  222.     long        total, contig;
  223.     EventRecord event;
  224.     short        count;
  225.     long        x;
  226.     OSErr        err;
  227.     unsigned long    now;
  228.  
  229.     gInBackground = false;
  230.     
  231.     InitGraf((Ptr) &qd.thePort);
  232.     InitFonts();
  233.     InitWindows();
  234.     InitMenus();
  235.     TEInit();
  236.     InitDialogs(nil);
  237.     InitCursor();
  238.     
  239.     InstallAEHandlers();
  240.     menuBar = GetNewMBar(rMenuBar);            /* read menus into menu bar */
  241.     if ( menuBar == nil ) ExitToShell();
  242.     SetMenuBar(menuBar);                    /* install menus */
  243.     DisposeHandle(menuBar);
  244.     AppendResMenu(GetMenuHandle(mApple), 'DRVR');    /* add DA names to Apple menu */
  245.     DrawMenuBar();
  246.     
  247.     InitOpenTransport();
  248.     
  249.     gHasPowerManager = GotPowerManager();
  250.     
  251.     for (count = 1; count <= 3; count++)
  252.     {
  253.         if (WaitNextEvent(everyEvent, &event, 5, nil))
  254.         {
  255.             if (event.what==kHighLevelEvent)
  256.             {
  257.             err = AEProcessAppleEvent( &event ) ;
  258.             }
  259.  
  260.         }
  261.     }
  262. } /*Initialize*/
  263.  
  264. #pragma segment Main
  265.  
  266.